#!/bin/bash
# Stop the server

# Get time via founction
Rd() {
  date +"%Y-%m-%d %H:%M:%S"
}


# Log event
echo "[$(Rd)] Server shut down script is runing." >> /home/pi/scripts/log.txt
# End Log event 


sleep 2

screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 5 minutes for planned maintenance.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say      Estimate downtime 30 Minutes\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down timmer on." >> /home/pi/scripts/log.txt
# End Log event 
# start timmer

# 4m
sleep 60
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 4 minutes.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 4 minutes." >> /home/pi/scripts/log.txt
# End Log event 

# 3m
sleep 60
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 3 minutes.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 3 minutes." >> /home/pi/scripts/log.txt
# End Log event 

# 2m
sleep 60
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 2 minutes.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 2 minutes." >> /home/pi/scripts/log.txt
# End Log event 

# 1m
sleep 60
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 1 minutes.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 1 minutes." >> /home/pi/scripts/log.txt
# End Log event 

# 30s
sleep 30
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 30 seconds.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 30 seconds." >> /home/pi/scripts/log.txt
# End Log event 

# 10s
sleep 30
screen -S picraft -p 0 -X stuff 'say ========= Public Announcement  =========\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say The server is shutting down in 10 seconds.\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say =====================================\n'
# Log event
echo "[$(Rd)] Server shutting down in 10 seconds." >> /home/pi/scripts/log.txt
# End Log event 

# 5...1s
sleep 10
screen -S picraft -p 0 -X stuff 'say Shutting down in 5\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say Shutting down in 4\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say Shutting down in 3\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say Shutting down in 2\n'
sleep 1
screen -S picraft -p 0 -X stuff 'say Shutting down in 1\n'

sleep 1
# erver shutdown
screen -S picraft -p 0 -X stuff 'stop\n'

# Log event
echo "[$(Rd)] Server shutdown." >> /home/pi/scripts/log.txt
# End Log event 